package com.scala.zhenghui

import scala.collection._

/**
 * Created by Administrator on 2018/1/21/021.
 */
object MapDemo {
  def main(args: Array[String]) {



    // createMap()
    // visitMap
    // updateMap
    // showMapTuple
    // sortedMapTest
    tupleDemo
  }

  def createMap(): Unit = {
    //创建一个不可变的Map
    val ages = Map("Leo" -> 30, "Jen" -> 25, "Jack" -> 23)
    val ages1 = Map(("Leo", 30), ("Jen", 25), ("Jack", 23))
    //创建一个可变的Map
    val ages2 = scala.collection.mutable.Map("Leo" -> 30, "Jen" -> 25, "Jack" -> 23)
    ages2("Leo") = 23
    val ages3 = new mutable.HashMap[String, Int]()
  }


  def visitMap(): Unit = {
    val ages = scala.collection.mutable.Map("Leo" -> 30, "Jen" -> 25, "Jack" -> 23)
    val age = ages("Leo")
    println(age)
    val age2 = if (ages.contains("leo")) {
      println(age)
      ages("leo")
    } else {
      0
    }
    println(age2)
    val age3 = ages.getOrElse("leo", 0)
  }


  def updateMap(): Unit = {
    val ages = scala.collection.mutable.Map("Leo" -> 30, "Jen" -> 25, "Jack" -> 23)
    ages("Leo") = 31
    println(ages("Leo"))
    ages +=(("Mike", 34), ("Tom", 33))
    ages -= "Mike"
  }

  def showMapTuple(): Unit = {
    val ages = scala.collection.mutable.Map("Leo" -> 30, "Jen" -> 25, "Jack" -> 23)
    ages("Leo") = 31
    println(ages("Leo"))
    ages +=(("Mike", 34), ("Tom", 33))
    ages -= "Mike"
    for ((key, value) <- ages) {
      println(key + ":" + value)
    }
    for (key <- ages.keySet) {
      println(key)
    }
    for (value <- ages.values) {
      println(value)
    }
    val agesR = for ((key, value) <- ages) yield {
      (value, key)
    }
    for ((key, value) <- agesR) {
      println(key + ":" + value)
    }
  }


  def sortedMapTest(): Unit = {
    val ages = scala.collection.immutable.SortedMap("Leo" -> 30, "Jen" -> 25, "Jack" -> 23)
    println(ages("Leo"))
    for ((key, value) <- ages) {
      println(key + ":" + value)
    }
  }


  def LinkHashMapTest(): Unit = {
    val ages = scala.collection.mutable.LinkedHashMap("Leo" -> 30, "Jen" -> 25, "Jack" -> 23)
    println(ages("Leo"))
    for ((key, value) <- ages) {
      println(key + ":" + value)
    }
  }

  def zipTest(): Unit = {
    val names = Array("leo", "jack", "jen")
    val ages = Array(22, 34, 55)
    val nameAges = names.zip(ages)
    for ((key, value) <- nameAges) {
      println(key + ":" + value)
    }
  }

  def tupleDemo(): Unit ={
    var t = ("leo",30,"china")
    println(t._3)
  }


}
